package com.cangshi.controller;

import com.cangshi.entity.China;
import com.cangshi.entity.CorrectJSONObject;
import com.cangshi.entity.JSONException;
import com.cangshi.service.ChinaService;
import com.cangshi.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Eoly on 2016/12/22.
 */

@Controller
public class InitController {

    @Autowired
    ChinaService chinaService;

    @ResponseBody
    @RequestMapping(value = "/index", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String init() {
        return "你好你看见我了？";
    }


    @ResponseBody
    @RequestMapping(value = "/show-china", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String showChina(@RequestParam(required = false) Integer pid) {

        if (pid == null) {
            pid = 0;
        }

        List<China> chinaList = chinaService.findByPid(pid);

        if (chinaList.isEmpty()) {
            // 抛错
            throw new JSONException(55, "没有找到对应城市/县/区");
        }

        String meg = "找到" + chinaList.size() + "个城市/县/区";

        // 封装json对象并返回
        CorrectJSONObject correctJSONObject = new CorrectJSONObject(meg);
        correctJSONObject.put("dataList",chinaList);
        return correctJSONObject.toString();
    }

}
